﻿using MetroDataApi.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MetroDataApi.Controllers.DataQueryTable
{
    public class PointsChangeTableController : Controller
    {
        private JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings()
        {
            ContractResolver = new CamelCasePropertyNamesContractResolver(),
            Converters = new List<JsonConverter> { new MyDateTimeConvertor() }
        };

        public ActionResult Index(int prjId, string names = "", string endTime = "",
            string startTime = "", string order = "asc", string monitorType = "沉降", string location = "上行线", string json = "0")
        {
            var namelist = new List<string>();
            if (names.Length > 0)
            {
                namelist = names.Split(',').ToList();
            }
            else
            {
                using (var db = new MetroDataDataContext())
                {
                    namelist = db.MonitorPointsDictionary.
                        Where(p => p.MonitorType == monitorType && p.Location == location && p.PrjId == prjId).
                        Select(p => p.Name).ToList();
                }
            }

            if (json == "0")
            {

                var times = new List<DateTime>();
                var abc = this.getPtsListByNames002(prjId, namelist, endTime, startTime, order);
                ViewBag.Items = abc;
                ViewBag.Names = namelist;
                ViewBag.Times = abc.Select(p => p.First(i => i != null).DateTime.Value.ToString("yyyy-MM-dd")).ToList();
                return View();
            }
            else
            {
                return RedirectToAction("Json", new
                {
                    prjId = prjId,
                    names = names,
                    startTime = startTime,
                    endTime = endTime,
                    order = order,
                    monitorType = monitorType,
                    location = location
                });
            }
        }

        public ContentResult Json(int prjId, string names = "", string endTime = "", 
            string startTime = "", string order = "asc", string monitorType = "上行线", string location = "沉降")
        {
            var namelist = new List<string>();
            if (names.Length > 0)
            {
                namelist = names.Split(',').ToList();
            }
            else
            {
                using (var db = new MetroDataDataContext())
                {
                    namelist = db.MonitorPointsDictionary.
                        Where(p => p.MonitorType == monitorType && p.Location == location && p.PrjId == prjId).
                        Select(p => p.Name).ToList();
                }
            }
            var items = this.getPtsListByNames002(prjId, namelist, endTime, startTime, order);
            return Content(JsonConvert.SerializeObject(items, Formatting.Indented, jsonSerializerSettings), "application/json");
        }


        private List<List<MonitorPoints>> getPtsListByNames002(int prjId, List<string> namelist,
            string endTime = "", string startTime = "", string order = "asc")
        {
            var l = new List<List<MonitorPoints>>();
            var end = endTime.Length > 0 ? DateTime.Parse(endTime) : DateTime.Now;
            var start = startTime.Length > 0 ? DateTime.Parse(startTime) : DateTime.Parse("1990-1-1");
            using (var db = new MetroDataDataContext())
            {


                IQueryable<DateTime> gg = null;
                if (order == "asc")
                {
                    gg = from p in db.MonitorPoints
                         where p.PrjId == prjId && p.DateTime.Value <= end && p.DateTime.Value >= start
                         group p by p.DateTime.Value into g
                         orderby g.Key
                         select g.Key.Date;
                }
                else
                {
                    gg = from p in db.MonitorPoints
                         where p.PrjId == prjId && p.DateTime.Value <= end && p.DateTime.Value >= start
                         group p by p.DateTime.Value.Date into g
                         orderby g.Key descending
                         select g.Key.Date;
                }


                foreach (var t in gg)
                {
                    var item = (from n in namelist
                                join
                                q in
                                    (from p in db.MonitorPoints
                                     where p.DateTime.Value.Date == t && p.PrjId == prjId
                                     select p)
                                on n equals q.Name into gs
                                from gi in gs.DefaultIfEmpty()
                                select gi).ToList();

                    if (item.Count(p => p != null) > 0)
                    {
                        l.Add(item);
                    }
                }
                return l;

            }
        }



    }
}
